package com.cjzuji.rental.util;

import com.cjzuji.rental.Bean.RenRZuRequest;
import org.springframework.lang.Nullable;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;

/**
 * @author bag
 * @version V1.0
 * @date 2025/7/4 16:09
 * @since 1.0
 */

public class Tool {


    /**
     * 发送http请求
     * @param url 请求url
     * @param authorization 认证报文
     * @param reqBody  请求体
     * @return response
     * post
     */
    public static String request(String url, String accesstoken,String authorization, String reqBody, String orderNo, String azjtk){
        String response = "";
        PrintWriter out = null;
        BufferedReader in = null;
        try{
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();
            HttpURLConnection httpUrlConnection = (HttpURLConnection) conn;
            httpUrlConnection.setRequestProperty("Content-Type", "application/json");
            if (!azjtk.isEmpty()){
                httpUrlConnection.setRequestProperty("azjtk", azjtk);
            }
            if (!authorization.isEmpty()){
                httpUrlConnection.setRequestProperty("authorization",authorization);
            }
            if (!accesstoken.isEmpty()){
                httpUrlConnection.setRequestProperty("accesstoken",accesstoken);
            }
            if (!orderNo.isEmpty()){
                httpUrlConnection.setRequestProperty("orderno",orderNo);
            }
            httpUrlConnection.setDoOutput(true);
            httpUrlConnection.setDoInput(true);
            out = new PrintWriter(httpUrlConnection.getOutputStream());
            out.write(reqBody);
            out.flush();
            httpUrlConnection.connect();
            in = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                response += line;
            }
        }catch(Exception e){
            e.printStackTrace();
        } finally {
            try {
                if (out != null) { out.close();}
                if (in != null) {in.close();}
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return response;
    }

    public static String request(String url, String cookie,String token, String reqBody, String orderNo){
        String response = "";
        PrintWriter out = null;
        BufferedReader in = null;
        try{
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();
            HttpURLConnection httpUrlConnection = (HttpURLConnection) conn;
            httpUrlConnection.setRequestProperty("Content-Type", "application/json");
            if (!cookie.isEmpty()){
                httpUrlConnection.setRequestProperty("cookie",cookie);
            }
            if (!token.isEmpty()){
                httpUrlConnection.setRequestProperty("token",token);
            }
            if (!orderNo.isEmpty()){
                httpUrlConnection.setRequestProperty("orderno",orderNo);
            }
            httpUrlConnection.setDoOutput(true);
            httpUrlConnection.setDoInput(true);
            out = new PrintWriter(httpUrlConnection.getOutputStream());
            out.write(reqBody);
            out.flush();
            httpUrlConnection.connect();
            in = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                response += line;
            }
        }catch(Exception e){
            e.printStackTrace();
        } finally {
            try {
                if (out != null) { out.close();}
                if (in != null) {in.close();}
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return response;
    }


    /**
     * 发送GET请求
     * @param url 请求地址
     * @param accessToken 访问令牌(可选)
     * @param authorization 授权信息(可选)
     * @return 响应内容
     */

    public static String getRequest(String url, String accessToken, String authorization, String azjtk, String cookie) {
        StringBuilder response = new StringBuilder();
        BufferedReader in = null;
        try {
            URL realUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();

            // 设置请求方法
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type", "text/html; charset=UTF-8");

            // 添加可选头部
            if (authorization != null && !authorization.isEmpty()) {
                connection.setRequestProperty("Authorization", authorization);
            }
            if (accessToken != null && !accessToken.isEmpty()) {
                connection.setRequestProperty("accessToken", accessToken);
            }
            if (azjtk != null && !azjtk.isEmpty()) {
                connection.setRequestProperty("Azjtk", azjtk);
            }

            if (cookie != null && !cookie.isEmpty()) {
                connection.setRequestProperty("cookie", cookie);
            }

            // 设置连接和读取超时(可选)
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);

            // 建立连接
            connection.connect();

            // 检查响应码
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                in = new BufferedReader(
                        new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
                String line;
                while ((line = in.readLine()) != null) {
                    response.append(line);
                }
            } else {
                // 可以处理错误响应，这里简单返回状态码
                return "Error: HTTP response code " + responseCode;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return response.toString();
    }
    // 请求体get请求
    public static String getRequest(String url, String accessToken, String authorization, String azjtk, String cookie, RenRZuRequest reqBody) {
        StringBuilder response = new StringBuilder();
        BufferedReader in = null;
        try {
            // 拼接到地址后面
            url += "?page_id=" + reqBody.getPage_id() +"&frontEndCacheSalesAfterWo=10004&order_group_ids=%5B%5D&order_status=%5B%5D&order_type=%5B%5D&list_type=1&server_sort_type=1&returning_sort_type=1&wait_send_part_order=0&ctrl_ver=1&type=all&per_page=15&page=" + reqBody.getPage();
            System.out.println(url);
            URL realUrl = new URL(url);

            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();

            // 设置请求方法
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

            // 添加可选头部
            if (authorization != null && !authorization.isEmpty()) {
                connection.setRequestProperty("Authorization", authorization);
            }
            if (accessToken != null && !accessToken.isEmpty()) {
                connection.setRequestProperty("accessToken", accessToken);
            }
            if (azjtk != null && !azjtk.isEmpty()) {
                connection.setRequestProperty("Azjtk", azjtk);
            }

            if (cookie != null && !cookie.isEmpty()) {
                connection.setRequestProperty("cookie", cookie);
            }

            // 设置连接和读取超时(可选)
            connection.setConnectTimeout(80000);
            connection.setReadTimeout(80000);

            // 建立连接
            connection.connect();

            // 检查响应码
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                in = new BufferedReader(
                        new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
                String line;
                while ((line = in.readLine()) != null) {
                    response.append(line);
                }
            } else {
                // 可以处理错误响应，这里简单返回状态码
                return "Error: HTTP response code " + responseCode;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return response.toString();
    }

    public static String getRequestzy(String url, String accessToken, String authorization, String azjtk, String cookie, RenRZuRequest reqBody) {
        StringBuilder response = new StringBuilder();
        BufferedReader in = null;
        try {
            // 拼接到地址后面
            url += "?page_id=" + reqBody.getPage_id() +"&frontEndCacheSalesAfterWo=10004&name=&order_group_ids=%5B%5D&order_status=%5B%5D&order_type=%5B%5D&list_type=1&server_sort_type=1&returning_sort_type=1&wait_send_part_order=0&ctrl_ver=1&type=wait_return&per_page=15&page=" + reqBody.getPage();
            System.out.println(url);
            URL realUrl = new URL(url);

            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();

            // 设置请求方法
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

            // 添加可选头部
            if (authorization != null && !authorization.isEmpty()) {
                connection.setRequestProperty("Authorization", authorization);
            }
            if (accessToken != null && !accessToken.isEmpty()) {
                connection.setRequestProperty("accessToken", accessToken);
            }
            if (azjtk != null && !azjtk.isEmpty()) {
                connection.setRequestProperty("Azjtk", azjtk);
            }

            if (cookie != null && !cookie.isEmpty()) {
                connection.setRequestProperty("cookie", cookie);
            }

            // 设置连接和读取超时(可选)
            connection.setConnectTimeout(80000);
            connection.setReadTimeout(80000);

            // 建立连接
            connection.connect();

            // 检查响应码
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                in = new BufferedReader(
                        new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
                String line;
                while ((line = in.readLine()) != null) {
                    response.append(line);
                }
            } else {
                // 可以处理错误响应，这里简单返回状态码
                return "Error: HTTP response code " + responseCode;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return response.toString();
    }


    public static String getRequest(String url, String accessToken, String authorization, String azjtk) {
        StringBuilder response = new StringBuilder();
        BufferedReader in = null;

        try {
            URL realUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();

            // 设置请求方法
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Accept", "application/json");

            // 添加可选头部
            if (authorization != null && !authorization.isEmpty()) {
                connection.setRequestProperty("Authorization", authorization);
            }
            if (accessToken != null && !accessToken.isEmpty()) {
                connection.setRequestProperty("accessToken", accessToken);
            }
            if (azjtk != null && !azjtk.isEmpty()) {
                connection.setRequestProperty("Azjtk", azjtk);
            }

            // 设置连接和读取超时(可选)
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);

            // 建立连接
            connection.connect();

            // 检查响应码
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                in = new BufferedReader(
                        new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
                String line;
                while ((line = in.readLine()) != null) {
                    response.append(line);
                }
            } else {
                // 可以处理错误响应，这里简单返回状态码
                return "Error: HTTP response code " + responseCode;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return response.toString();
    }
    public static String getRequest(String url, String cookie, String token) {
        StringBuilder response = new StringBuilder();
        BufferedReader in = null;

        try {
            URL realUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();

            // 设置请求方法
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Accept", "application/json");

            // 添加可选头部
            if (cookie != null && !cookie.isEmpty()) {
                connection.setRequestProperty("cookie", cookie);
            }
            if (token != null && !token.isEmpty()) {
                connection.setRequestProperty("token", token);
            }

            // 设置连接和读取超时(可选)
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);

            // 建立连接
            connection.connect();

            // 检查响应码
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                in = new BufferedReader(
                        new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
                String line;
                while ((line = in.readLine()) != null) {
                    response.append(line);
                }
            } else {
                // 可以处理错误响应，这里简单返回状态码
                return "Error: HTTP response code " + responseCode;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return response.toString();
    }

}

